using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;

namespace ARF.Web.Controls
{
    public class BasicContactForm : BaseForm
    {
        protected override void OnLoad(EventArgs e)
        {
            AddButtonHandler("idOK", new EventHandler(okButton_Click));

            base.OnLoad(e);
        }

        void okButton_Click(object sender, EventArgs e)
        {
            OnOK();
        }

        protected virtual void OnOK()
        {
            try
            {
                string sSiteUrl = SPContext.Current.Site.Url;

                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    using (SPSite oSite = new SPSite(sSiteUrl))
                    {
                        SPList oContactList = oSite.RootWeb.Lists["ContactMessages"];
                        if (oContactList == null) throw new Exception("Unable to locate 'ContactMessages' list");

                        oSite.RootWeb.AllowUnsafeUpdates = true;

                        SPListItem oComment = oContactList.Items.Add();
                        oComment["Title"] = GetTextBox("idSubject").Text;
                        oComment["Message"] = GetTextBox("idMessage").Text;
                        oComment["ContactName"] = GetTextBox("idName").Text;
                        oComment["ContactEmail"] = GetTextBox("idEmail").Text;
                        oComment["Category"] = GetDropList("idCategory").SelectedValue;

                        oComment.Update();
                    }

                });

                ShowPanel("idThankyouPanel", true);
                ShowPanel("idFormPanel", false);
                ShowPanel("idIntroPanel", false);

            }
            catch (Exception ex)
            {
                Log.Trace("Unable to add contact messge");
                Log.Trace(ex);
                ShowPanel("idFormPanel", false);
                ShowPanel("idIntroPanel", false);
                ShowPanel("idErrorPanel", true);
            }
        }
    }
}
